﻿using API.Common;
using API.Constant;
using API.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace API.Controllers.Api
{
    public class TypeController : ApiController
    {
        private DataLinqDataContext db = new DataLinqDataContext();
        private Dictionary<string, object> ResponseStatus = new Dictionary<string, object>();
        private Dictionary<string, object> returnData = new Dictionary<string, object>();
        private ReturnStatusConstant returnStatus;

        [HttpGet]
        [ActionName("GetAllType")]
        public HttpResponseMessage GetAllType()
        {
            if (ModelState.IsValid)
            {
                var checkEtoken = APICommon.CheckEtoken(Request.Headers);
                if (checkEtoken)
                {
                    var result = (from res in db.tbl_Types
                                  select new Types
                                  {
                                      Id = res.id,
                                      TypeName = res.TypeName
                                  }).ToList();
                    if (result != null)
                    {
                        var returnStatus = new ReturnStatusConstant(ErrorCode.ErrorCodeNone, Status.Success, MessageResponse.MessageNone, APICommon.Etoken);
                        returnData.Add("returnTypeName", result);
                        returnData.Add("status", returnStatus);
                        return Request.CreateResponse(HttpStatusCode.Created, returnData);
                    }
                    else
                    {
                        returnStatus = new ReturnStatusConstant(ErrorCode.ErrorCodeInvalidUserName, Status.Faiure, MessageResponse.MessageInvalidUser, string.Empty);
                        returnData.Add("status", returnStatus);
                        return Request.CreateResponse(HttpStatusCode.Created, returnData);
                    }
                }
                else
                {
                    returnStatus = new ReturnStatusConstant(ErrorCode.ErrorCodeInvalidEtoken, Status.Faiure, MessageResponse.MessageInvalidEtoken, string.Empty);
                    returnData.Add("status", returnStatus);
                    return Request.CreateResponse(HttpStatusCode.Created, returnData);
                }
            }
            return null;
        }
    }
}
